from enum import unique

from django.db import models

from wxcloudrun.models import BaseModel


@unique
class UserDeliveryOrderState(models.IntegerChoices):
    """发货状态"""

    unshipped = 0  # 未发货
    delivered = 1  # 已发货
    received = 2  # 已收货
    cancel = 3  # 取消


@unique
class UserPaymentType(models.IntegerChoices):
    """支付类型"""

    unpaid = 0  # 未付款
    weixin = 1  # 微信
    stored_money = 2  # 余额
    hunjing = 3  # 魂晶
    mixture = 4  # 混合
    free = 5  # 免运费


class UserDeliveryOrder(BaseModel):
    """发货订单表"""

    user_uuid = models.UUIDField(verbose_name="用户uuid")
    user_express_info_uuid = models.UUIDField(verbose_name="用户收货地址uuid")
    orderid = models.CharField(max_length=40, verbose_name="订单号")
    state = models.PositiveIntegerField(
        verbose_name="订单状态",
        choices=UserDeliveryOrderState.choices,
        default=UserDeliveryOrderState.unshipped,
    )
    pay_type = models.PositiveIntegerField(
        verbose_name="支付类型",
        choices=UserPaymentType.choices,
        default=UserPaymentType.unpaid,
    )
    pay_datetime = models.DateTimeField(verbose_name="支付时间", null=True, default=None)
    delivery_datetime = models.DateTimeField(
        verbose_name="发货时间", null=True, default=None
    )
    receive_datetime = models.DateTimeField(
        verbose_name="收货时间", null=True, default=None
    )
    sum_count = models.PositiveIntegerField(verbose_name="赏品数量", default=0)
    sum_price = models.PositiveIntegerField(verbose_name="赏品总价", default=0)
    sum_recovery_price = models.PositiveIntegerField(verbose_name="赏品总成本价", default=0)
    delivery_price = models.PositiveIntegerField(verbose_name="运费", default=0)
    coupon_price = models.PositiveIntegerField(verbose_name="优惠劵", default=0)
    actual_price = models.PositiveIntegerField(verbose_name="实付", default=0)
    delivery_name = models.CharField(max_length=200, verbose_name="物流公司")
    delivery_number = models.CharField(max_length=100, verbose_name="物流单号")
    remark = models.CharField(max_length=255, verbose_name="留言", default="")

    class Meta:
        db_table = "UserDeliveryOrder"  # 数据库表名
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["createdAt"]),
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["user_express_info_uuid"]),
            models.Index(fields=["orderid"]),
        ]
        ordering = ["-createdAt"]


class UserDeliveryOrderGoods(BaseModel):
    """发货订单关联物品"""

    delivery_order_uuid = models.UUIDField(verbose_name="发货订单uuid")
    user_package_uuid = models.UUIDField(verbose_name="赏袋物品uuid")
    good_uuid = models.UUIDField(verbose_name="物品uuid")

    class Meta:
        db_table = "UserDeliveryOrderGoods"  # 数据库表名
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["createdAt"]),
            models.Index(fields=["delivery_order_uuid"]),
            models.Index(fields=["user_package_uuid"]),
            models.Index(fields=["good_uuid"]),
        ]
        ordering = ["-createdAt"]


class UserExpress(BaseModel):  # 收货信息
    user_uuid = models.UUIDField(verbose_name="用户uuid")
    default_uuid = models.UUIDField(verbose_name="默认收货地址信息uuid", null=True)

    class Meta:
        db_table = "UserExpress"  # 数据库表名
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["createdAt"]),
            models.Index(fields=["user_uuid"]),
        ]
        ordering = ["createdAt"]


class UserExpressInfo(BaseModel):  # 收货详细信息
    express_uuid = models.UUIDField(verbose_name="UserExpress关联uuid")
    user_uuid = models.UUIDField(verbose_name="用户uuid")
    express_name = models.CharField(max_length=255, verbose_name="收件人姓名")
    express_phone = models.CharField(max_length=20, verbose_name="收件人电话")
    express_area = models.CharField(max_length=200, verbose_name="收件人地区")
    express_address = models.CharField(max_length=255, verbose_name="收件人详细地址")

    class Meta:
        db_table = "UserExpressInfo"  # 数据库表名
        indexes = [
            models.Index(fields=["uuid"]),
            models.Index(fields=["deleted"]),
            models.Index(fields=["createdAt"]),
            models.Index(fields=["express_uuid"]),
            models.Index(fields=["user_uuid"]),
            models.Index(fields=["express_name"]),
            models.Index(fields=["express_phone"]),
            models.Index(fields=["express_area"]),
        ]
        ordering = ["createdAt"]
